package com.example.city_home_app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Bean.Down_UserInfo;
import com.example.city_home_app.Fragment.Fragment_userinfo_info;
import com.example.city_home_app.Fragment.Fragment_userinfo_moment;
import com.example.city_home_app.Fragment.Fragment_userinfo_post;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInfoActivity extends BaseActivity {
    @BindView(R.id.img_userinfo_userimage)
    MyCircleImageView imgUserinfoUserimage;
    @BindView(R.id.tv_userinfo_username)
    TextView tvUserinfoUsername;
    @BindView(R.id.tv_userinfo_userintro)
    TextView tvUserinfoUserintro;
    @BindView(R.id.tv_userinfo_userfocus)
    TextView tvUserinfoUserfocus;
    @BindView(R.id.ll_userinfo_moment)
    LinearLayout llUserinfoMoment;
    @BindView(R.id.ll_userinfo_post)
    LinearLayout llUserinfoPost;
    @BindView(R.id.ll_userinfo_info)
    LinearLayout llUserinfoInfo;
    @BindView(R.id.userinfo_vp_content)
    FrameLayout userinfoVpContent;
    @BindView(R.id.ll_userinfo_focus)
    LinearLayout llUserinfoFocus;
    @BindView(R.id.ll_userinfo_msg)
    LinearLayout llUserinfoMsg;
    @BindView(R.id.tv_userinfo_moment)
    TextView tvUserinfoMoment;
    @BindView(R.id.tv_userinfo_post)
    TextView tvUserinfoPost;
    @BindView(R.id.tv_userinfo_info)
    TextView tvUserinfoInfo;
    @BindView(R.id.img_userinfo_focus)
    ImageView imgUserinfoFocus;
    @BindView(R.id.tv_userinfo_focus)
    TextView tvUserinfoFocus;
    @BindView(R.id.img_userinfo_edit)
    ImageView imgUserinfoEdit;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Fragment fragment_moment, fragment_post, fragment_info;
    private int userid, focuscount, fanscount;
    private String haedurl,username;
    private Down_UserInfo userInfo;
    private boolean isfocus = false;
    private ApiModel apiModel = new ApiModel();

    public static UserInfoActivity instance;

    public int getUserid() {
        return userid;
    }

    public Down_UserInfo getUserInfo() {
        return userInfo;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_userinfo;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);

        instance = this;
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        userid = intent.getIntExtra("id", 0);
        apiModel.getuserinfo(userid, userInfoRetrofitListener);
        if (!(userid == BaseActivity.getUserinfo().getUserid())) {
            apiModel.isfocususer(BaseActivity.getUserinfo().getUserid(), userid, isfocuslistener);
        } else {
            imgUserinfoEdit.setVisibility(View.VISIBLE);
        }
        selectPage(0);
    }


    private void selectPage(int select) {
        resetView();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (select) {
            case 0:
                if (fragment_moment == null) {
                    fragment_moment = new Fragment_userinfo_moment();
                    fragmentTransaction.add(R.id.userinfo_vp_content, fragment_moment);
                } else {
                    fragmentTransaction.show(fragment_moment);
                }
                tvUserinfoMoment.setTextColor(0xff49afcd);
                break;
            case 1:
                if (fragment_post == null) {
                    fragment_post = new Fragment_userinfo_post();
                    fragmentTransaction.add(R.id.userinfo_vp_content, fragment_post);
                } else {
                    fragmentTransaction.show(fragment_post);
                }
                tvUserinfoPost.setTextColor(0xff49afcd);
                break;
            case 2:
                if (fragment_info == null) {
                    fragment_info = new Fragment_userinfo_info();
                    fragmentTransaction.add(R.id.userinfo_vp_content, fragment_info);
                } else {
                    fragmentTransaction.show(fragment_info);
                }
                tvUserinfoInfo.setTextColor(0xff49afcd);
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_moment != null) {
            fragmentTransaction.hide(fragment_moment);
        }
        if (fragment_post != null) {
            fragmentTransaction.hide(fragment_post);
        }
        if (fragment_info != null) {
            fragmentTransaction.hide(fragment_info);
        }
    }

    private void resetView() {
        tvUserinfoMoment.setTextColor(0xff929292);
        tvUserinfoPost.setTextColor(0xff929292);
        tvUserinfoInfo.setTextColor(0xff929292);
    }

    private void updatefocus() {
        tvUserinfoUserfocus.setText("关注 " + focuscount + "  |  粉丝 " + fanscount);
    }

    @OnClick({R.id.img_userinfo_userimage, R.id.ll_userinfo_moment, R.id.ll_userinfo_post, R.id.ll_userinfo_info, R.id.ll_userinfo_focus, R.id.ll_userinfo_msg,R.id.img_userinfo_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_userinfo_userimage:
                ArrayList<String> imgurls = new ArrayList<>();
                imgurls.add(haedurl);
                Intent intent = new Intent(this, PhotoViewActivity.class);
                intent.putStringArrayListExtra("urls",imgurls);
                intent.putExtra("currentPosition", 0);
                startActivity(intent);
                break;
            case R.id.ll_userinfo_moment:
                selectPage(0);
                break;
            case R.id.ll_userinfo_post:
                selectPage(1);
                break;
            case R.id.ll_userinfo_info:
                selectPage(2);
                break;
            case R.id.ll_userinfo_focus:
                if(userid==0){
                    UIUtils.showToast(this, "无法关注", UIUtils.LENGTH_SHORT);
                }else {
                    if (!(userid == BaseActivity.getUserinfo().getUserid())) {
                        if (isfocus) {
                            apiModel.unfocususer(BaseActivity.getUserinfo().getUserid(), userid, unfocuslistener);
                        } else {
                            apiModel.focususer(BaseActivity.getUserinfo().getUserid(), userid, focuslistener);
                        }
                    } else {
                        UIUtils.showToast(this, "不能关注自己", UIUtils.LENGTH_SHORT);
                    }
                }
                break;
            case R.id.ll_userinfo_msg:
                if(userid==0){
                    UIUtils.showToast(this, "无法发送私信", UIUtils.LENGTH_SHORT);
                }else {
                    if (!(userid == BaseActivity.getUserinfo().getUserid())) {
                        Intent intent1 = new Intent(this, MessageActivity.class);
                        intent1.putExtra("sendid", userid);
                        intent1.putExtra("sendname", username);
                        startActivity(intent1);
                    } else {
                        UIUtils.showToast(this, "不能对自己发送私信", UIUtils.LENGTH_SHORT);
                    }
                }
                break;
            case R.id.img_userinfo_edit:
                Intent intent1 = new Intent(this,EditActivity.class);
                startActivity(intent1);
                break;
        }
    }

    private RetrofitListener<Down_UserInfo> userInfoRetrofitListener = new RetrofitListener<Down_UserInfo>() {
        @Override
        public void onResponse(Down_UserInfo listener) {
            haedurl = listener.getUser_headimage();
            userInfo = listener;
            username = listener.getUser_name();
            tvUserinfoUsername.setText(listener.getUser_name());
            tvUserinfoUserintro.setText(listener.getUser_intro());
            focuscount = listener.getFocuscount();
            fanscount = listener.getFfocuscount();
            updatefocus();
            Picasso.with(UserInfoActivity.this)
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + listener.getUser_headimage())
                    .into(imgUserinfoUserimage);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> isfocuslistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if (listener.getMessage().equals("success")) {
                imgUserinfoFocus.setImageResource(R.drawable.ic_no);
                isfocus = true;
                tvUserinfoFocus.setText("取消关注");
            } else {
                imgUserinfoFocus.setImageResource(R.drawable.ic_add_userinfo);
                isfocus = false;
                tvUserinfoFocus.setText("关注");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> focuslistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if (listener.getMessage().equals("success")) {
                imgUserinfoFocus.setImageResource(R.drawable.ic_no);
                isfocus = true;
                fanscount++;
                updatefocus();
                tvUserinfoFocus.setText("取消关注");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> unfocuslistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if (listener.getMessage().equals("success")) {
                imgUserinfoFocus.setImageResource(R.drawable.ic_add_userinfo);
                isfocus = false;
                fanscount--;
                updatefocus();
                tvUserinfoFocus.setText("关注");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };



}
